
#include "mynetwork.h"
#include "config.h"
#include "WiFi.h"
#include "arduino.h"
#include  "esp_log.h"

#define TAG "MyNet"
// IPAddress eth_dns_backup[2];

//  void WiFiEvent(WiFiEvent_t event)
// {
//     switch (event) {
//         case SYSTEM_EVENT_WIFI_READY: 
//             Serial.printf("WiFi interface ready\n");
//             break;
//         case SYSTEM_EVENT_SCAN_DONE:
//             Serial.printf("Completed scan for access points\n");
//             break;
//         case SYSTEM_EVENT_STA_START:
//             Serial.printf("WiFi client started\n");
//             break;
//         case SYSTEM_EVENT_STA_STOP:
//             Serial.printf("WiFi clients stopped\n");
//             break;
//         case SYSTEM_EVENT_STA_CONNECTED:
//             Serial.printf("Connected to access point\n");
//             break;
//         case SYSTEM_EVENT_STA_DISCONNECTED:
//              Serial.printf("Disconnected from WiFi access point\n");
//             break;
//         case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
//             Serial.printf("Authentication mode of access point has changed\n");
//             break;
//         case SYSTEM_EVENT_STA_GOT_IP:
//             Serial.printf("ssid: %s\n",WiFi.SSID().c_str());
//             Serial.printf("pwd: %s\n",WiFi.psk().c_str());
//             Serial.printf("Obtained IP address: ");
//             Serial.printf("%s\n",WiFi.localIP().toString().c_str());
//             connectToMqtt();
// 			break;
//         case SYSTEM_EVENT_STA_LOST_IP:
//             Serial.printf("Lost IP address and IP address is reset to 0\n");
//             break;
//         case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
//             Serial.printf("WiFi Protected Setup (WPS): succeeded in enrollee mode\n");
//             break;
//         case SYSTEM_EVENT_STA_WPS_ER_FAILED:
//             Serial.printf("WiFi Protected Setup (WPS): failed in enrollee mode\n");
//             break;
//         case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
//             Serial.printf("WiFi Protected Setup (WPS): timeout in enrollee mode\n");
//             break;
//         case SYSTEM_EVENT_STA_WPS_ER_PIN:
//             Serial.printf("WiFi Protected Setup (WPS): pin code in enrollee mode\n");
//             break;
//         case SYSTEM_EVENT_AP_START:
//             Serial.printf("WiFi access point started\n");
//             break;
//         case SYSTEM_EVENT_AP_STOP:
//             Serial.printf("WiFi access point  stopped\n");
//             break;
//         case SYSTEM_EVENT_AP_STACONNECTED:
//             Serial.printf("Client connected\n");
//             break;
//         case SYSTEM_EVENT_AP_STADISCONNECTED:
//             Serial.printf("Client disconnected\n");
//             break;
//         case SYSTEM_EVENT_AP_STAIPASSIGNED:
//             Serial.printf("Assigned IP address to client\n");
//             break;
//         case SYSTEM_EVENT_AP_PROBEREQRECVED:
//             Serial.printf("Received probe request\n");
//             break;        
//         case SYSTEM_EVENT_ROC_DONE:/*!< Indicates the completion of Remain-on-Channel operation status */
//             Serial.printf("SYSTEM_EVENT_ROC_DONE \n");
//             if(config.get_wifi_mode() == 0)
//             {
              
//             }
//             break;
//         case SYSTEM_EVENT_GOT_IP6:
//             Serial.printf("IPv6 is preferred\n");
//             break;
//         case SYSTEM_EVENT_ETH_START:
//             Serial.printf("Ethernet started\n");
//             break;
//         case SYSTEM_EVENT_ETH_STOP:
//             Serial.printf("Ethernet stopped\n");
//             break;
//         case SYSTEM_EVENT_ETH_CONNECTED:
//             Serial.printf("Ethernet connected\n");
//             break;
//         case SYSTEM_EVENT_ETH_DISCONNECTED:
//             Serial.printf("Ethernet disconnected\n");
//             break;
//         case SYSTEM_EVENT_ETH_GOT_IP:
//             Serial.printf("SYSTEM_EVENT_ETH_GOT_IP Obtained IP address!!!!!!!!!!!!\n");
//             break;
//         default: break;
//     }
// }



void network_init()
{
  
//   WiFi.onEvent(WiFiEvent);
  if(config.get_wifi_mode() == 0)
  {
    WiFi.setAutoReconnect(true); // 设置自动连接
    WiFi.begin(config.get_wifi_ssid().c_str(), config.get_wifi_pwd().c_str());
  }
  else if(config.get_wifi_mode() == 1)
  {
    WiFi.setAutoReconnect(true); // 设置自动连接
    WiFi.begin(config.get_wifi_ssid().c_str(), config.get_wifi_pwd().c_str());
    WiFi.config(config.get_wifi_ip(),config.get_wifi_gw(),config.get_wifi_subnet(),config.get_dns(0),config.get_dns(1));
  }

  ESP_LOGI(TAG, "WiFi mac:%s\n",WiFi.macAddress().c_str());
}


// uint32_t last_wifi_disconnect_time;
// int wifi_retry = 0;
// int keeper_state = 0;
// void wifi_keeper()
// {
//   // 如果WiFi没有连接且不在配网状态，每隔60s连接一次,
//   if (millis() - last_wifi_disconnect_time > 60 * 1000)
//   {
//     last_wifi_disconnect_time = millis();
//     // Serial.printf("[wifi_keeper]:WiFi.status()= %d; millis=%d\n", WiFi.status(), millis() / 1000);
//     if (WiFi.status() != WL_CONNECTED)
//     {
//       WiFi.disconnect();
//       delay(100);
//       WiFi.begin();
//       wifi_retry++;
//       Serial.printf("[wifi_keeper]:WiFi.begin(),wifi_retry = %d; millis=%d\n", wifi_retry, millis() / 1000);
//     }
//   }
// }

// void network_loop()
// {
//   if(config.get_wifi_mode() != 2)
//   {
//     wifi_keeper();
//   }
// }
















// void print_wakeup_reason(){
//   esp_sleep_wakeup_cause_t wakeup_reason;

//   wakeup_reason = esp_sleep_get_wakeup_cause();
//   switch(wakeup_reason)
//   {
//     case ESP_SLEEP_WAKEUP_EXT0 : Serial.printf("Wakeup caused by external signal using RTC_IO"); break;
//     case ESP_SLEEP_WAKEUP_EXT1 : Serial.printf("Wakeup caused by external signal using RTC_CNTL"); break;
//     case ESP_SLEEP_WAKEUP_TIMER : Serial.printf("Wakeup caused by timer"); break;
//     case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.printf("Wakeup caused by touchpad"); break;
//     case ESP_SLEEP_WAKEUP_ULP : Serial.printf("Wakeup caused by ULP program"); break;
//     default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
//   }
// }
// void print_reset_reason(int reason)
// {
//   switch ( reason)
//   {
//     case 1 : Serial.printf ("POWERON_RESET");break;          /**<1,  Vbat power on reset*/
//     case 3 : Serial.printf ("SW_RESET");break;               /**<3,  Software reset digital core*/
//     case 4 : Serial.printf ("OWDT_RESET");break;             /**<4,  Legacy watch dog reset digital core*/
//     case 5 : Serial.printf ("DEEPSLEEP_RESET");break;        /**<5,  Deep Sleep reset digital core*/
//     case 6 : Serial.printf ("SDIO_RESET");break;             /**<6,  Reset by SLC module, reset digital core*/
//     case 7 : Serial.printf ("TG0WDT_SYS_RESET");break;       /**<7,  Timer Group0 Watch dog reset digital core*/
//     case 8 : Serial.printf ("TG1WDT_SYS_RESET");break;       /**<8,  Timer Group1 Watch dog reset digital core*/
//     case 9 : Serial.printf ("RTCWDT_SYS_RESET");break;       /**<9,  RTC Watch dog Reset digital core*/
//     case 10 : Serial.printf ("INTRUSION_RESET");break;       /**<10, Instrusion tested to reset CPU*/
//     case 11 : Serial.printf ("TGWDT_CPU_RESET");break;       /**<11, Time Group reset CPU*/
//     case 12 : Serial.printf ("SW_CPU_RESET");break;          /**<12, Software reset CPU*/
//     case 13 : Serial.printf ("RTCWDT_CPU_RESET");break;      /**<13, RTC Watch dog Reset CPU*/
//     case 14 : Serial.printf ("EXT_CPU_RESET");break;         /**<14, for APP CPU, reseted by PRO CPU*/
//     case 15 : Serial.printf ("RTCWDT_BROWN_OUT_RESET");break;/**<15, Reset when the vdd voltage is not stable*/
//     case 16 : Serial.printf ("RTCWDT_RTC_RESET");break;      /**<16, RTC Watch dog reset digital core and rtc module*/
//     default : Serial.printf ("NO_MEAN");
//   }
// }
// void verbose_print_reset_reason(int reason)
// {
//   switch ( reason)
//   {
//     case 1  : Serial.printf ("Vbat power on reset");break;
//     case 3  : Serial.printf ("Software reset digital core");break;
//     case 4  : Serial.printf ("Legacy watch dog reset digital core");break;
//     case 5  : Serial.printf ("Deep Sleep reset digital core");break;
//     case 6  : Serial.printf ("Reset by SLC module, reset digital core");break;
//     case 7  : Serial.printf ("Timer Group0 Watch dog reset digital core");break;
//     case 8  : Serial.printf ("Timer Group1 Watch dog reset digital core");break;
//     case 9  : Serial.printf ("RTC Watch dog Reset digital core");break;
//     case 10 : Serial.printf ("Instrusion tested to reset CPU");break;
//     case 11 : Serial.printf ("Time Group reset CPU");break;
//     case 12 : Serial.printf ("Software reset CPU");break;
//     case 13 : Serial.printf ("RTC Watch dog Reset CPU");break;
//     case 14 : Serial.printf ("for APP CPU, reseted by PRO CPU");break;
//     case 15 : Serial.printf ("Reset when the vdd voltage is not stable");break;
//     case 16 : Serial.printf ("RTC Watch dog reset digital core and rtc module");break;
//     default : Serial.printf ("NO_MEAN");
//   }
// }
